from bs4 import BeautifulSoup
import time
import os
import datetime
import json

import urllib.request

from TycDetailParse.spiders.parseinfo.branch_info import get_branch_info
from TycDetailParse.spiders.parseinfo.config import MONGO_HOST, MONGO_PORT, MONOG_DB, MONGO_USERNAME, MONOG_PASSWORD
from TycDetailParse.spiders.parseinfo.court_info import get_court_report_info
from TycDetailParse.spiders.parseinfo.getcertificate import get_certificate
from TycDetailParse.spiders.parseinfo.getswlevel import get_swlevel
from TycDetailParse.spiders.parseinfo.getzhaopin import get_zhaopin
from TycDetailParse.spiders.parseinfo.gudong_info import get_gudong_info
from TycDetailParse.spiders.parseinfo.invest_info import get_invest_info
from TycDetailParse.spiders.parseinfo.jingpin_info import get_jingpin_info
from TycDetailParse.spiders.parseinfo.mongodb_util import MongoDBUtil
import pymongo

from TycDetailParse.spiders.parseinfo.partner_info import get_partner_info
from TycDetailParse.spiders.parseinfo.punish_info import get_punish_info
from TycDetailParse.spiders.parseinfo.rongzi_info import get_rongzi_info
from TycDetailParse.spiders.parseinfo.team_info import get_team_info
from TycDetailParse.spiders.parseinfo.tongzi_info import get_touzhi_info
from TycDetailParse.spiders.parseinfo.websiterecord import get_site
from TycDetailParse.spiders.parseinfo.trademarkinformation import get_pright
from TycDetailParse.spiders.parseinfo.patentinformation import get_zhuanli
from TycDetailParse.spiders.parseinfo.softwarecopyright import get_soft_copyright
from TycDetailParse.spiders.parseinfo.weixin import get_weixin
from TycDetailParse.spiders.parseinfo.workcopyright import get_work_copyright
from TycDetailParse.spiders.parseinfo.productinformation import get_product

from TycDetailParse.spiders.parseinfo.base_info import get_base_info

mongo_instance = MongoDBUtil(host=MONGO_HOST,
                             port=MONGO_PORT,
                             user=MONGO_USERNAME,
                             password=MONOG_PASSWORD, db_name=MONOG_DB).database


def get_html(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    return html


# 1-工商基本数据-2018-05-26
def get_base(soup, cid, company):
    print('start base*******')
    d_company_gsxt_1m = {}
    com_site = soup.find('a', class_="company-link").text if soup.find('a',
                                                                       class_="company-link") else ''
    com_mobile = ''
    com_telephone = ''
    com_email = ''
    area = ''
    com_legal = ''
    if soup.find('span', class_="address"):
        if len(soup.find('span', class_="address").text) > 6:
            area = soup.find('span', class_="address")['title']
    if soup.findAll('span', class_='hidden'):
        try:
            phone = soup.findAll('span', class_='hidden')[1].text
            com_telephone = phone
        except KeyError:
            pass
        # .replace('[','')  # .next_sibling.text.replace('[', '')
        # phone = phone.replace(']', '')
        # phone = phone.replace('"', '')
        # com_mobile = phone

    if soup.find('span', class_='pl5'):
        com_email = soup.find('span', class_='pl5').find('script').text
        com_email = com_email.replace(']', '')
        com_email = com_email.replace('[', '')
        com_email = com_email.replace('"', '')
    if soup.find('div', class_='humancompany'):
        com_legal = soup.find('div', class_='humancompany').find('div', class_='name').find('a').text
    introduce = soup.find('script', id='company_base_info_detail').text.strip() if soup.find('script',
                                                                                             id='company_base_info_detail') else ''
    com_office = area
    try:
        if soup.find('div', class_='logo -w100').find('img'):
            icon = soup.find('div', class_='logo -w100').find('img')['data-src']
    except AttributeError:
        print('no logo')
    create_at = int(time.time())
    update_at = int(time.time())
    if soup.find('div', class_='num-opening'):
        com_state = soup.find('div', class_='num-opening').text
    else:
        com_state = '注销'
    # 基础信息
    if soup.find('div', id='_container_baseInfo'):
        if soup.find('div', id='_container_baseInfo').find('table', class_='table -striped-col -border-top-none'):
            trs = soup.find('div', id='_container_baseInfo').find('table',
                                                                  class_='table -striped-col -border-top-none').find_all(
                'tr')
            d_company_gsxt_1m['regist_code'] = trs[0].find_all('td')[1].text
            d_company_gsxt_1m['org_code'] = trs[0].find_all('td')[3].text
            d_company_gsxt_1m['credit_code'] = trs[1].find_all('td')[1].text
            d_company_gsxt_1m['com_type'] = trs[1].find_all('td')[3].text
            d_company_gsxt_1m['sw_code'] = trs[2].find_all('td')[1].text
            d_company_gsxt_1m['industry'] = trs[2].find_all('td')[3].text
            d_company_gsxt_1m['end_date'] = trs[3].find_all('td')[1].text
            d_company_gsxt_1m['regist_date'] = trs[3].find_all('td')[3].text
            d_company_gsxt_1m['nashui_zizhi'] = trs[4].find_all('td')[1].text
            d_company_gsxt_1m['staff_num'] = trs[4].find_all('td')[3].text
            d_company_gsxt_1m['shijiao'] = trs[5].find_all('td')[1].text
            d_company_gsxt_1m['regist_authority'] = trs[5].find_all('td')[3].text
            d_company_gsxt_1m['canbao_num'] = trs[6].find_all('td')[1].text
            d_company_gsxt_1m['english_name'] = trs[6].find_all('td')[3].text

            d_company_gsxt_1m['company_id'] = cid

            # business,regist_capital_source,issue_date,regist_date = get_qichacha(company,cid)
            # business,regist_capital_source,issue_date,regist_date = get_baidu(company,cid)
            # d_company_gsxt_1m['business'] = business
            # d_company_gsxt_1m['regist_capital_source'] = regist_capital_source
            # d_company_gsxt_1m['issue_date'] = issue_date
            # d_company_gsxt_1m['regist_date'] = regist_date

            d_company_gsxt_1m['company'] = company
            d_company_gsxt_1m['com_site'] = com_site
            d_company_gsxt_1m['area'] = area
            d_company_gsxt_1m['com_mobile'] = com_mobile
            d_company_gsxt_1m['com_telephone'] = com_telephone
            d_company_gsxt_1m['com_email'] = com_email
            d_company_gsxt_1m['com_legal'] = com_legal
            d_company_gsxt_1m['introduce'] = introduce
            d_company_gsxt_1m['com_office'] = com_office
            # d_company_gsxt_1m['regist_date'] = regist_date

            d_company_gsxt_1m['icon'] = icon
            d_company_gsxt_1m['create_at'] = create_at
            d_company_gsxt_1m['update_at'] = update_at
            d_company_gsxt_1m['com_state'] = com_state

            print('success base*******')
            print(d_company_gsxt_1m)
            mongo_instance.base.update({'company_id': cid}, d_company_gsxt_1m, True)


# 2-主要成员数据-2018-05-26
def get_partner(soup, cid, company):
    print('start 主要成员数据*******')
    imps = soup.find('div', id="_container_staff").find('tbody').find_all('tr') if soup.find('div',
                                                                                             id="_container_staff") else []
    items = {}
    for people in imps:
        ahrefs = people.find_all('a') if people.find('a') else []
        if ahrefs:
            partner_name = ahrefs[0].text
            try:
                company_num = ahrefs[1].text.strip().strip('>')
                tds = people.find_all('td')
                partner_duty = tds[2].text
                items = {'company': company, 'company_id': cid, 'partner_duty': partner_duty,
                         'partner_name': partner_name,
                         'company_num': company_num}
                items['create_at'] = int(time.time())
                items['update_at'] = items['create_at']
                mongo_instance.partner.insert(items)
            except IndexError as e:
                print(e, cid, company)

    print('success 主要成员数据*******')


# 3-股东数据-2018-05-26
def get_gudong(soup, cid, company):
    print('start 股东数据*******')
    imps = soup.find('div', id="_container_holder").find('tbody').find_all('tr') if soup.find('div',
                                                                                              id="_container_holder") else []
    items = {}
    for people in imps:
        ahrefs = people.find_all('a') if people.find('a') else []
        if ahrefs:
            try:
                company_name = ahrefs[0].text
                company_num = ahrefs[1].text.strip().strip('>')
                tds = people.find_all('td')
                money_bilv = tds[2].text
                money_renjiao = tds[3].find('span').text
                items = {'company': company, 'company_id': cid, 'name': company_name, 'company_num': company_num,
                         'company_num': company_num, 'money_bilv': money_bilv, 'money_renjiao': money_renjiao}
                items['create_at'] = int(time.time())
                items['update_at'] = items['create_at']
            except IndexError:
                print('Index Error')
            else:
                mongo_instance.gudong.insert(items)
    print('sucess 股东数据*******')


# 4-对外投资-2018-05-26
def get_invest(soup, cid, company):
    print('start 对外投资数据*******')
    for tr in soup.find('div', id='_container_invest').find('tbody').find_all('tr') if soup.find('div',
                                                                                                 id='_container_invest') else []:
        item = {}
        tds = tr.find_all('td')
        if len(tds) > 7:
            item['to_company'] = tds[3].text
            if tds[4].find('a'):
                item['com_legal'] = tds[4].find('a', class_='link-click').text
            else:
                item['com_legal'] = tds[4].text
            item['regist_capital'] = tds[5].text
            item['invest_ratio'] = tds[6].text
            item['regist_date'] = tds[7].text
            item['com_status'] = tds[8].text
            item['create_at'] = int(time.time())
            item['update_at'] = item['create_at']
            item['company'] = company
            item['company_id'] = cid
            print(item)
            mongo_instance.invest.insert(item)
    print('success 对外投资数据*******')


# 5-变更记录（历史记录）-2018-05-26
def get_uplog(soup, cid, company):
    print('start 变更记录数据*******')
    for tr in soup.find('div', id='_container_changeinfo').find('tbody').find_all('tr') if soup.find('div',
                                                                                                     id='_container_changeinfo') else []:
        item = {}
        tds = tr.find_all('td')
        item['update_date'] = tds[1].text
        item['update_name'] = tds[2].text
        item['before_update'] = tds[3].text
        item['last_update'] = tds[4].text
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        print(item)
        mongo_instance.uplog.insert(item)
    print('success 变更记录数据*******')


# 5-变更记录（企查查-历史记录）-
def get_uplog2(soup, cid, company):
    print('start 变更记录数据*******')
    try:
        table_trs = soup.find('section', id='Changelist').find('table', class_='ntable').find_all('tr')

        for tr in table_trs:
            item = {}
            tds = tr.find_all('td')
            if (tds):
                item['update_date'] = tds[1].text
                item['update_name'] = tds[2].text
                item['before_update'] = tds[3].text
                item['last_update'] = tds[4].text
                item['create_at'] = int(time.time())
                item['update_at'] = item['create_at']
                item['company'] = company
                item['company_id'] = cid
                print(item)
                mongo_instance.uplog2.insert(item)
        print('success 变更记录数据*******')
    except Exception as e:
        print(e, company)


# 6-公司年报数据-2018-05-26
def get_report(soup, cid, company):
    print('start 公司年报数据*******')
    # find_all('a', attrs={"event-name": '企业详情-企业年报'})
    if soup.find('div', class_='report-item-list'):
        links = soup.find('div', class_='report-item-list').find_all('a', class_='link-click')

        for tr in set(links):
            link = tr['href']
            text = get_html(link)
            report_soup = BeautifulSoup(text, 'lxml')
            item = {}
            try:
                if report_soup.find('h1', class_='report_all_title mt0'):
                    item['title'] = report_soup.find('h1', class_='report_all_title mt0').text
                    item['year'] = link.split('/')[-1]
                    base_tds = [td.text for td in report_soup.find('div', class_='report_baseInfo').find_all('td')][
                               1::2]
                    base_feilds = ['regist_code', 'company', 'telephone', 'code', 'com_state', 'staff_num', 'email',
                                   'is_web',
                                   'address', 'is_touzhi']
                    for index in range(len(base_tds)):
                        item[base_feilds[index]] = base_tds[index]

                    property_tds = [td.text for td in report_soup.find('div', class_='report_property').find_all('td')][
                                   1::2]
                    property_feilds = ['total_capital', 'owner_equity', 'total_sell', 'total_profit', 'main_business',
                                       'net_profit',
                                       'total_tax', 'total_liabilities']
                    for index in range(len(property_tds)):
                        item[property_feilds[index]] = property_tds[index]
                    item['from_url'] = link
                    item['create_at'] = int(time.time())
                    item['update_at'] = item['create_at']
                    item['company'] = company
                    item['company_id'] = cid
                    print(item)
                    mongo_instance.report.insert(item)
                    get_report_gudong(report_soup, cid, company, item['year'])
                    get_report_invest(report_soup, cid, company, item['year'])
            except AttributeError:
                print('司年报无数据')


# 7-分支机构-2018-05-26
def get_branch(soup, cid, company):
    print('start 分支机构数据*******')
    for tr in soup.find('div', id='_container_branch').find('tbody').find_all('tr') if soup.find('div',
                                                                                                 id='_container_branch') else []:
        item = {}
        tds = tr.find_all('td')
        if len(tds) > 5:
            item['branch_name'] = tds[3].text
            item['com_legal'] = tds[4].text
            item['status'] = tds[5].text
            item['regist_date'] = tds[6].text
            item['create_at'] = int(time.time())
            item['update_at'] = item['create_at']
            item['company'] = company
            item['company_id'] = cid
            print(item)
            mongo_instance.branch.insert(item)
    print('success 分支机构数据*******')


# 8-融资历史-2018-05-26(new)
def get_rongzi(soup, cid, company):
    print('start 融资数据*******')
    for tr in soup.find('div', id='_container_rongzi').find('tbody').find_all('tr') if soup.find('div',
                                                                                                 id='_container_rongzi') else []:
        item = {}
        tds = tr.find_all('td')
        if len(tds) > 4:
            item['date'] = tds[1].text
            item['lunci'] = tds[2].text
            item['guzhi'] = tds[3].text
            item['money'] = tds[4].text
            item['bilv'] = tds[5].text
            item['tzcompany'] = tds[6].text
            item['from'] = tds[7].text
            item['create_at'] = int(time.time())
            item['update_at'] = item['create_at']
            item['company'] = company
            item['company_id'] = cid
            print(item)
            mongo_instance.rongzi.insert(item)
    print('success 融资历史数据*******')


# 9-核心团队-2018-05-26(new)
def get_team(soup, cid, company):
    print('start 核心团队数据*******')
    for tr in soup.find('div', id='_container_teamMember').find_all('div', class_='team-item') if soup.find('div',
                                                                                                            id='_container_teamMember') else []:
        item = {}
        item['avatar'] = tr.find('img')['src']
        item['name'] = tr.find('img')['alt']
        item['duty'] = tr.find('div', class_='team-title').text
        item['intro'] = tr.find('div', class_='team-right').text
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        print(item)
        mongo_instance.team.insert(item)
    print('success 核心团队数据*******')


# 10-投资事件-2018-05-26(new)
def get_touzhi(soup, cid, company):
    print('start 投资事件数据*******')
    for tr in soup.find('div', id='_container_touzi').find('tbody').find_all('tr') if soup.find('div',
                                                                                                id='_container_touzi') else []:
        item = {}
        tds = tr.find_all('td')
        if len(tds) > 5:
            td5s = tds[5].find('tbody').find_all('td')
            product = td5s[1].text
            item['date'] = tds[1].text
            item['lunci'] = tds[2].text
            item['money'] = tds[3].text
            item['tzcompany'] = tds[4].text
            item['tzproduct'] = product
            item['area'] = tds[8].text
            item['hangye'] = tds[9].text
            item['yewu'] = tds[10].text
            item['create_at'] = int(time.time())
            item['update_at'] = item['create_at']
            item['company'] = company
            item['company_id'] = cid
            print(item)
            mongo_instance.touzhi.insert(item)
    print('sucess 投资事件*******')


# 11-企业业务-2018-05-26(new)
def get_yewu(soup, cid, company):
    print('start 企业业务数据*******')
    for tr in soup.find('div', id='_container_firmProduct').find_all('div', class_='product-item') if soup.find('div',
                                                                                                                id='_container_firmProduct') else []:
        item = {}
        item['pic'] = tr.find('img')['src']
        item['product_name'] = tr.find('img')['alt']
        item['hangye'] = tr.find('div', class_='hangye').text
        item['yewu'] = tr.find('div', class_='yeweu overflow-width').text
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        print(item)
        mongo_instance.yewu.insert(item)
    print('end 企业业务数据*******')


# 12-竞品信息-2018-05-26(new)
def get_jingpin(soup, cid, company):
    print('start 竞品信息*******')
    try:
        for tr in soup.find('div', id='_container_jingpin').find('tbody').find_all('tr') if soup.find('div',
                                                                                                      id='_container_jingpin') else []:
            item = {}
            tds = tr.find_all('td')

            if len(tds) > 6:
                if tds[1].find('tbody'):
                    item['name'] = tds[1].find('tbody').find_all('td')[1].text
                    item['icon'] = tds[1].find('tbody').find_all('td')[0].find('img')['src']
                    item['area'] = tds[4].text
                    item['lunci'] = tds[5].text
                    item['hangye'] = tds[6].text
                    item['yewu'] = tds[7].text
                    item['date'] = tds[8].text
                    item['guzhi'] = tds[9].text
                    item['create_at'] = int(time.time())
                    item['update_at'] = item['create_at']
                    item['company'] = company
                    item['company_id'] = cid
                    print(item)
                    mongo_instance.jingpin.insert(item)
        print('success 竞品信息*******')
    except Exception as e:
        print(e, company)


# 13-开庭公告-2018-05-26(new)
def get_court_notice(soup, cid, company):
    print('start 开庭公告数据*******')
    try:
        if soup.find('div', id='_container_announcementcourt').find('tbody'):
            for tr in soup.find('div', id='_container_announcementcourt').find('tbody').find_all('tr') if soup.find(
                    'div',
                    id='_container_announcementcourt') else []:
                item = {}
                tds = tr.find_all('td')
                # 详情json数据
                script = tds[5].find('script')
                item = json.loads(script.text)
                item['date'] = tds[1].text
                item['case'] = tds[2].text

                item['plaintiff'] = tds[3].text
                item['defendant'] = tds[4].text
                item['company_id'] = cid
                item['company'] = company
                item['create_at'] = int(time.time())
                item['update_at'] = item['create_at']
                print(item)
                mongo_instance.court_notice.insert(item)
        print('success 开庭公告数据*******')
    except  AttributeError:
        print('开庭公告 无数据')


# 14-法律诉讼-2018-05-26(new)
def get_laws(soup, cid, company):
    print('start 法律诉讼数据*******')
    for tr in soup.find('div', id='_container_lawsuit').find('tbody').find_all('tr') if soup.find('div',
                                                                                                  id='_container_lawsuit') else []:
        item = {}
        tds = tr.find_all('td')
        item['date'] = tds[1].text
        item['name'] = tds[2].find('a').text
        item['from_url'] = tds[2].find('a')['href']
        item['type'] = tds[3].text
        item['identity'] = tds[4].text
        item['code_num'] = tds[5].text

        from_text = get_html(item['from_url'])
        from_soup = BeautifulSoup(from_text, 'lxml')
        item['content'] = str(from_soup.contents[1])
        try:
            item['doc_url'] = from_soup.find('div', class_='f12 common-remark-style pb30 new-border-bottom').find('a')[
                'href']
        except:
            item['doc_url'] = ''
        item['company_id'] = cid
        item['company'] = company
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        print(item)
        mongo_instance.laws.insert(item)
    print('success 法律诉讼数据*******')


# 15-法院公告-2018-05-26(new)
def get_court_report(soup, cid, company):
    print('start 法院公告数据*******')
    for tr in soup.find('div', id='_container_court').find('tbody').find_all('tr') if soup.find('div',
                                                                                                id='_container_court') else []:
        item = {}
        tds = tr.find_all('td')
        # 详情json数据
        script = tds[6].find('script')
        if script is None:
            continue
        item = json.loads(script.text)
        item['date'] = tds[1].text
        if tds[2].find('a'):
            item['plaintiff'] = tds[2].find('a').text
            item['plaintiff_url'] = tds[2].find('a')['href']
        else:
            item['plaintiff'] = tds[2].find('span').text
        if tds[3].find('a'):
            item['defendant'] = tds[3].find('a').text
            item['defendant_url'] = tds[3].find('a')['href']
        else:
            item['defendant'] = tds[3].find('span').text
        item['type'] = tds[4].find('span').text
        item['court'] = tds[5].find('span').text

        item['company_id'] = cid
        item['company'] = company
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        print(item)
        mongo_instance.court_report.insert(item)
    print('success 法院公告数据*******')


# 16-行政处罚-2018-05-26(new)
def get_punish(soup, cid, company):
    print('start 行政处罚数据*******')
    for tr in soup.find('div', id='_container_punish').find('tbody').find_all('tr') if soup.find('div',
                                                                                                 id='_container_punish') else []:
        item = {}
        tds = tr.find_all('td')
        if len(tds) > 4:
            script = tds[5].find('script')
        else:
            script = tds[3].find('script')
        item = json.loads(script.text)
        item['details'] = script.text
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        print(item)
        mongo_instance.punish.insert(item)
    print('success 行政处罚数据*******')


# 17-招标数据-2018-05-26(new)
def get_zhaobiao(soup, cid, company):
    print('start 招标数据*******')
    for tr in soup.find('div', id='_container_bid').find('tbody').find_all('tr') if soup.find('div',
                                                                                              id='_container_bid') else []:
        item = {}
        tds = tr.find_all('td')
        item['date'] = tds[1].text
        item['title'] = tds[2].find('a').text
        new_url = tds[2].find('a')['href']
        # 详情
        text = get_html(new_url)
        report_soup = BeautifulSoup(text, 'lxml')
        try:
            item['from_url'] = \
                report_soup.find('div', class_='f12 common-remark-style pb30 new-border-bottom').find('a')['href']
            if report_soup.find('div', class_='vF_detail_content'):
                item['content'] = report_soup.find('div', class_='vF_detail_content').text
            else:
                item['content'] = report_soup.find('div', class_='lawsuitbox pt30 pb30').text
        except:
            item['from_url'] = ''
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        print(item)
        mongo_instance.zhaobiao.insert(item)
    print('success 招标数据*******')


# 20-进出口信用数据-2018-05-26
def get_credit(soup, cid, company):
    print('start 进出口信用数据*******')
    for tr in soup.find('div', id='_container_importAndExport').find('tbody').find_all('tr') if soup.find('div',
                                                                                                          id='_container_importAndExport') else []:
        item = {}
        tds = tr.find_all('td')
        item['org'] = tds[0].text  # 注册海关
        item['code'] = tds[1].text  # 海关编码
        item['type'] = tds[2].text  # 经营类别
        item['details'] = tds[3].find('script').text
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        print(item)
        mongo_instance.credit.insert(item)
    print('success 进出口信用数据*******')


# 29-年报股东及投资信息-2018-05-26
def get_report_gudong(soup, cid, company, year):
    print('start 年报股东及投资数据*******')
    for tr in soup.find('div', class_='report_holder').find('tbody').find_all('tr') if soup.find('div',
                                                                                                 class_='report_holder') else []:
        item = {}
        tds = tr.find_all('td')
        item['name'] = tds[0].text
        item['com_capital'] = tds[1].text
        item['com_date'] = tds[2].text
        item['com_type'] = tds[3].text
        item['real_capital'] = tds[4].text
        item['real_date'] = tds[5].text
        item['com_type2'] = tds[6].text
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        item['year'] = year
        print(item)
        mongo_instance.report_gudong.insert(item)
    print('success 年报股东及投资数据*******')


# 30-年报对外信息-2018-05-26
def get_report_invest(soup, cid, company, year):
    for tr in soup.find('div', class_='report_outbound').find('tbody').find_all('tr') if soup.find('div',
                                                                                                   class_='report_outbound') else []:
        item = {}
        tds = tr.find_all('td')
        item['code'] = tds[0].text
        item['company2'] = tds[1].text
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        item['company'] = company
        item['company_id'] = cid
        item['year'] = year
        print(item)
        print(item)
        mongo_instance.report_invest.insert(item)


def passmethod(fuction):
    try:
        fuction
    except Exception as e:
        print(e)


def get_all_info(soup, cid, company):
    # info = {}
    # info[''] = '===============  company info start ======================='
    # save_data.save2json(info)
    passmethod(get_base_info(mongo_instance, soup, cid, company))  # 1、工商信息数据
    passmethod(get_partner_info(mongo_instance, soup, cid, company))  # 2、主要成员数据
    passmethod(get_gudong_info(mongo_instance, soup, cid, company))  # 3、股东信息数据
    passmethod(get_invest_info(mongo_instance, soup, cid, company))  # 4、对外投资数据
    # passmethod(get_uplog(soup, cid, company))         # 5、变更记录数据
    # passmethod(get_report(soup, cid, company))        # 6、公司年报数据
    passmethod(get_branch_info(mongo_instance, soup, cid, company))  # 7、分支机构数据
    passmethod(get_rongzi_info(mongo_instance, soup, cid, company))  # 8、融资历史数据
    passmethod(get_team_info(mongo_instance, soup, cid, company))  # 9、核心团队数据
    passmethod(get_touzhi_info(mongo_instance, soup, cid, company))  # 10、投资事件数据
    passmethod(get_jingpin_info(mongo_instance, soup, cid, company))  # 12、竞品信息数据
    # passmethod(get_court_notice(soup, cid, company))  # 13、开庭公告数据
    # passmethod(get_laws(soup, cid, company))          # 14、法律诉讼数据
    passmethod(get_court_report_info(mongo_instance, soup, cid, company))  # 15、法院公告数据
    passmethod(get_punish_info(mongo_instance, soup, cid, company))  # 16、行政处罚数据
    # passmethod(get_zhaobiao(soup, cid, company))      # 17、招标数据
    passmethod(get_zhaopin(mongo_instance, soup, cid, company))  # 18、招聘数据数据
    passmethod(get_swlevel(mongo_instance, soup, cid, company))  # 19、税务评级数据
    passmethod(get_credit(soup, cid, company))  # 20、进出口信用数据
    passmethod(get_certificate(mongo_instance, soup, cid, company))  # 21、资质证书数据
    passmethod(get_weixin(mongo_instance, soup, cid, company))  # 22、微信公众号信息
    passmethod(get_pright(mongo_instance, soup, cid, company))  # 23、商标信息数据
    passmethod(get_soft_copyright(mongo_instance, soup, cid, company))  # 24、软件著作权数据
    passmethod(get_work_copyright(mongo_instance, soup, cid, company))  # 25、作品著作权数据
    passmethod(get_site(mongo_instance, soup, cid, company))  # 26、网站备案数据
    passmethod(get_product(mongo_instance, soup, cid, company))  # 27、产品信息数据
    passmethod(get_zhuanli(mongo_instance, soup, cid, company))  # 28、专利数据数据
    # info[''] = '===============  company info end ======================='
    # save_data.save2json(info)


def get_company_info(folder_path, cid):
    soup = BeautifulSoup(open(folder_path + cid + ".html", 'r', encoding='UTF-8'))
    # print(soup.prettify())
    cid = soup.find('meta', attrs={'name': 'mobile-agent'})['content'].split('/')[-1]
    company = soup.find('h1', class_='name').text.strip() if soup.find('h1', class_='name') else ''
    get_all_info(soup, cid, company)
